package com.org.zp.ctrl;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.zp.R;
import com.org.zp.app.RecApp;
import com.org.zp.ctrl.article.ArticleActivity;
import com.org.zp.ctrl.job.JobActivity;
import com.org.zp.ctrl.login.LoginActivity;
import com.org.zp.ctrl.more.MoreActivity;
import com.org.zp.ctrl.myzhaopin.ApplyRecordActivity;
import com.org.zp.ctrl.myzhaopin.CreateResumeActivity;
import com.org.zp.ctrl.myzhaopin.ResumeListActivity;
import com.org.zp.ctrl.myzhaopin.ZpHomeActivity;
import com.org.zp.ctrl.payquery.PayqueryActivity;
import com.org.zp.ctrl.search.SearchJobActivity;
import com.org.zp.entity.login.UserInfo;

public class UserMainActivity extends BaseActivity {

	private GridView gridview;
	private SimpleAdapter saImageItems;
	private ArrayList<HashMap<String, Object>> lstImageItem;
	private Button top_right_btn;

	private int[] pic;

	private boolean isExit = false;

	private RecApp app;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_user);

		if (!(ActivityTaskManager.getInstance()
				.containsName("UserMainActivity"))) {
			ActivityTaskManager.getInstance().putActivity("UserMainActivity",
					this);
		}

		app = getMyApplication();

		top_right_btn = (Button) findViewById(R.id.header_right);
		if ("".equals(app.user.getuID())) {
			top_right_btn.setText("登录");
		} else {
			top_right_btn.setText("注销");
		}

		top_right_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if ("登录".equals(top_right_btn.getText().toString())) {
					Intent intent = new Intent(UserMainActivity.this,
							LoginActivity.class);
					intent.putExtra("type", "0");
					startActivity(intent);
					ActivityTaskManager.getInstance().removeActivity(
							"UserMainActivity");
				} else {
					top_right_btn.setText("登录");
					app.user = new UserInfo();
					lstImageItem.clear();
					for (int i = 0; i < pic.length; i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						map.put("ItemImage", pic[i]);// 添加图像资源的ID
						lstImageItem.add(map);
					}
					saImageItems.notifyDataSetChanged();
				}

			}
		});

		pic = new int[] { R.drawable.main_search,
				R.drawable.main_precise_search, R.drawable.main_info,
				R.drawable.main_recom, R.drawable.main_user,
				R.drawable.main_resume, R.drawable.main_pay,
				R.drawable.main_apply, R.drawable.main_more };

		gridview = (GridView) findViewById(R.id.main_gridview);
		gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));

		lstImageItem = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < pic.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ItemImage", pic[i]);// 添加图像资源的ID
			lstImageItem.add(map);
		}
		// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
		saImageItems = new SimpleAdapter(this, // 没什么解释
				lstImageItem,// 数据来源
				R.layout.main_grid_item,// night_item的XML实现

				// 动态数组与ImageItem对应的子项
				new String[] { "ItemImage" },

				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.main_grid_img });
		// 添加并且显示
		gridview.setAdapter(saImageItems);
		// 添加消息处理
		gridview.setOnItemClickListener(new ItemClickListener());
	}

	// 当AdapterView被单击(触摸屏或者键盘)，则返回的Item单击事件
	class ItemClickListener implements OnItemClickListener {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			switch (arg2) {
			case 0:
				Intent intent0 = new Intent(UserMainActivity.this,
						SearchJobActivity.class);
				intent0.putExtra("full", false);
				startActivity(intent0);
				break;
			case 1:
				Intent intent1 = new Intent(UserMainActivity.this,
						SearchJobActivity.class);
				intent1.putExtra("full", true);
				startActivity(intent1);
				break;
			case 2:
				Intent intent2 = new Intent(UserMainActivity.this,
						JobActivity.class);
				startActivity(intent2);
				break;
			case 3:
				Intent intent3 = new Intent(UserMainActivity.this,
						ArticleActivity.class);
				startActivity(intent3);

				break;
			case 4:
				if (app.user.getuID().equals("")) {
					Intent intent4 = new Intent(UserMainActivity.this,
							LoginActivity.class);
					intent4.putExtra("type", "0");
					startActivity(intent4);
					ActivityTaskManager.getInstance().removeActivity(
							"UserMainActivity");
				} else {
					Intent intent4 = new Intent(UserMainActivity.this,
							ZpHomeActivity.class);
					startActivity(intent4);
				}

				break;
			case 5:
				if (app.user.getuID().equals("")) {
					Intent intent5 = new Intent(UserMainActivity.this,
							LoginActivity.class);
					intent5.putExtra("type", "0");
					startActivity(intent5);
					ActivityTaskManager.getInstance().removeActivity(
							"UserMainActivity");
				} else {
					if (app.user.getResume().size() == 0) {
						Intent intent5 = new Intent(UserMainActivity.this,
								CreateResumeActivity.class);
						startActivity(intent5);
					} else {
						Intent intent5 = new Intent(UserMainActivity.this,
								ResumeListActivity.class);
						startActivity(intent5);
					}

				}
				break;
			case 6:
				Intent intent6 = new Intent(UserMainActivity.this,
						PayqueryActivity.class);
				startActivity(intent6);

				break;
			case 7:
				if (app.user.getuID().equals("")) {
					Intent intent7 = new Intent(UserMainActivity.this,
							LoginActivity.class);
					intent7.putExtra("type", "0");
					startActivity(intent7);
					ActivityTaskManager.getInstance().removeActivity(
							"UserMainActivity");
				} else {
					Intent intent7 = new Intent(UserMainActivity.this,
							ApplyRecordActivity.class);
					startActivity(intent7);
				}

				break;
			case 8:
				Intent intent8 = new Intent(UserMainActivity.this,
						MoreActivity.class);
				startActivity(intent8);
				break;
			}
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			/*
			 * QuitDialog dialog = new QuitDialog(UserMainActivity.this);
			 * dialog.exitConfirm();
			 */
			exit();

			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void exit() {
		if (!isExit) {
			isExit = true;
			Toast.makeText(getApplicationContext(), "再按一次退出程序",
					Toast.LENGTH_SHORT).show();
			mHandler.sendEmptyMessageDelayed(0, 2000);
		} else {
			ActivityTaskManager.getInstance().closeAllActivity();
		}
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			isExit = false;
		}

	};

}
